//
//  HMStatusCell.swift
//  WeiboTT
//
//  Created by 王嘉涛 on 2016/11/17.
//  Copyright © 2016年 王嘉涛. All rights reserved.
//

import UIKit
private let margin: CGFloat = 8
//配图视图之间的间距
private let imageMargin: CGFloat = 2
private let maxWidth = ScreenWidth - 2 * margin
private let itemWidth = (maxWidth - 2 * imageMargin) / 3

class HMStatusCell: UITableViewCell {
    // 用户头像
    @IBOutlet weak var iconImage: UIImageView!
    // 用户名称
    @IBOutlet weak var nameLable: UILabel!
    // 用户等级
    @IBOutlet weak var mbRankImage: UIImageView!
    // 微博时间
    @IBOutlet weak var timeLable: UILabel!
    // 微博来源
    @IBOutlet weak var sourceLable: UILabel!
    // 微博正文
    @IBOutlet weak var contentLable: UILabel!
    // 微博用户认证类型
    @IBOutlet weak var avatarView: UIImageView!
    // 配图视图
    @IBOutlet weak var pictureView: HMPictureView!
    // 高度约束
    @IBOutlet weak var pictureHeightCons: NSLayoutConstraint!
    // 宽度约束
    @IBOutlet weak var pictureWidthCons: NSLayoutConstraint!
    // 试图配图的布局对象
    @IBOutlet weak var pictureViewFlowLayout: UICollectionViewFlowLayout!
    // 转发label
    @IBOutlet weak var zhuanFaLbl: UILabel!
    // 图片和转发label之间的约束
    @IBOutlet weak var pictureViewTopCons: NSLayoutConstraint!
    // 底部toolBar
    @IBOutlet weak var toolBar: UIView!
    // cell需要绑定的数据
    var viewmodel : HMStatusViewModel? {
        didSet{
            // 设置子控件的数据
            iconImage.sd_setImage(with: viewmodel?.iconURL)
            nameLable.text = viewmodel?.status?.user?.name
            timeLable.text = viewmodel?.status?.created_at
            sourceLable.text = viewmodel?.status?.source
            contentLable.text = viewmodel?.status?.text
            avatarView.image = viewmodel?.verifitedImage
            mbRankImage.image = viewmodel?.mbRankImage
            
            let count = viewmodel?.status?.pic_urls?.count ?? 0
            //根据count值 修改顶部约束对象的值
            pictureViewTopCons.constant = (count == 0 ? 0 : margin)
            let pSize = calculatePictureViewSize(count: count)
            // 根据约束约束对象的约束值
            pictureWidthCons.constant = pSize.width
            pictureHeightCons.constant = pSize.height
            // 给配图视图设置数据源
            pictureView.pictureInfos = viewmodel?.status?.pic_urls
            zhuanFaLbl?.text = viewmodel?.status?.retweeted_status?.text

        }
        
    }
    
    // 根据图片的张数计算配图视图的大小,跟新配图视图的宽高约束对象的值
    private func calculatePictureViewSize(count : Int) -> CGSize {
        if count == 0 {
            return CGSize.zero
        }
        
        // 判断四张图片
        if count == 4 {
            let width = 2 * itemWidth + margin
            return CGSize(width: width, height: width)
        }
        
        let row = CGFloat((count - 1) / 3) + 1
        let h = row * itemWidth + (row - 1) * imageMargin
        return CGSize(width: maxWidth, height: h)
    }
    
    // 创建xib初始化方法
    override func awakeFromNib() {
        super.awakeFromNib()
        // 设置背景颜色
        self.contentView.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)
        // 设置行数为0
        self.contentLable.numberOfLines = 0
        // 最大宽度
        self.contentLable.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
        self.zhuanFaLbl?.numberOfLines = 0
        //设置最大的布局宽度
        //print(self.bounds)
//        self.zhuanFaLbl?.preferredMaxLayoutWidth = ScreenWidth  - 2 *  margin
        // 设置流水布局
        pictureViewFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        // 设置航和列最小间距
        pictureViewFlowLayout.minimumLineSpacing = imageMargin
        pictureViewFlowLayout.minimumInteritemSpacing = imageMargin
        // 选中的样式
        selectionStyle = .none
    }

    
}
